const cloud = require("@cloudbase/node-sdk");

const app = cloud.init({
    env: cloud.SYMBOL_CURRENT_ENV,
});

var db = app.database();
const _ = db.command
const ZXData = db.collection('ZX-Data')

// 获取openId云函数入口函数
exports.main = async (event, context) => {

    // return await AION_ZB_WQ.get()
    var params = {}
    var name = event.data.name
    var sex = event.data.sex
    var levelMin = event.data.levelMin
    var levelMax = event.data.levelMax
    var pinzhi = event.data.pinzhi

    var className = event.data.class
    var type = event.data.type


    if (name)
        params.name = db.RegExp({
            regexp: String(name),
            options: 'i',
        })
    if (className)
        params.class = className
    if (sex)
        params.sex = sex
    if (pinzhi)
        params.pinzhi = pinzhi
    
    if (levelMin || levelMax)
        params.level = _.gte(levelMin).and(_.lte(levelMax))
    
    if(type)
        params.type = type
    
    
    const res = await ZXData.where(params)
    const list = (await res
        .skip((event.data.pageIndex - 1) * event.data.pageSize)
        .limit(event.data.pageSize)
        .get()).data

    const total = (await res.count()).total

    return {
        list: list,
        total: total,
    }
    
    
};